package com.itheima.controller;

import com.itheima.domain.PmsBrand;
import com.itheima.service.db.PmsBrandService;
import com.itheima.util.R;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.*;


@RestController
@RequestMapping("/brand")
public class PmsBrandController {
    @DubboReference
    PmsBrandService pmsBrandService;

    //根据品牌名称分页获取品牌列表
    @GetMapping("/list")
    public R findByCondition(
            @RequestParam(required = false) String keyword,
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize) {
        return R.success(pmsBrandService.findByPageAndConddtion(keyword, pageNum, pageSize));
    }

    //添加品牌
    @PostMapping("/create")
    public R create(@RequestBody PmsBrand pmsBrand) {
        pmsBrandService.save(pmsBrand);
        return R.success("添加成功");
    }

    //更新品牌
    @PostMapping("/update/{id}")
    public R update(@PathVariable("id") Integer id, @RequestBody PmsBrand brand) {
        pmsBrandService.update(brand);
        return R.success("修改成功");
    }

    //获取全部品牌列表
    @GetMapping("/listAll")
    public R getAll() {
        return R.success(pmsBrandService.getAll());
    }

    //批量更新厂家制造商状态
    @PostMapping("/update/factoryStatus")
    public R updateFactoryStatus(@RequestParam Integer[] ids, @RequestParam Integer factoryStatus) {
        pmsBrandService.updateFactoryStatus(ids, factoryStatus);
        return R.success("修改成功");
    }

    //批量更新显示状态
    @PostMapping("/update/showStatus")
    public R updateStatus(@RequestParam Integer[] ids, Integer showStatus) {
        pmsBrandService.updateStatus(ids, showStatus);
        return R.success("");
    }

    //根据编号查询品牌信息
    @GetMapping("/{id}")
    public R getById(@PathVariable(value = "id") Integer id) {
        return R.success(pmsBrandService.findById(id));
    }

    //删除品牌
    @GetMapping("/delete/{id}")
    public R deleteById(@PathVariable Long id) {
        pmsBrandService.deleteById(id);
        return R.success("删除成功");
    }

    //批量删除品牌
    @PostMapping("/delete/batch")
    public R deleteBatch(@RequestParam Long[] ids) {
        pmsBrandService.deleteByIds(ids);
        return R.success("删除成功");
    }
}
